package com.qianyitian.hope2.analyzer.service;

import com.alibaba.fastjson.JSON;
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import com.qianyitian.hope2.analyzer.config.PropertyConfig;
import com.qianyitian.hope2.analyzer.model.Stock;
import com.qianyitian.hope2.analyzer.model.SymbolList;
import com.qianyitian.hope2.sdk.Securities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class StockService extends AbstractStockService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    PropertyConfig propertyConfig;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public Stock getStockDaily(String symbol) {
        Stock stock = null;
        try {
            stock = super.getStockDaily(symbol);
        } catch (Exception e) {
            logger.error("stock not exist " + symbol, e);
        }
        return stock;
    }

    @Override
    public Stock getStockDailySA(String symbol) {
        Stock stock = null;
        try {
            stock = super.getStockDailySA(symbol);
        } catch (Exception e) {
            logger.error("stock not exist " + symbol, e);
        }
        return stock;
    }


    @Override
    public SymbolList getSymbols(String portfolio) {
        if (portfolio == null) {
            return restTemplate.getForObject(propertyConfig.getStockService() + "/stockList/", SymbolList.class);
        } else {
            return restTemplate.getForObject(propertyConfig.getStockService() + "/stockList/" + portfolio, SymbolList.class);
        }
    }

    @Override
    public SymbolList getSymbols(String portfolio, boolean removeST) {
        SymbolList symbolList = null;
        if (portfolio == null) {
            symbolList = restTemplate.getForObject(propertyConfig.getStockService() + "/stockList/", SymbolList.class);
        } else {
            symbolList = restTemplate.getForObject(propertyConfig.getStockService() + "/stockList/" + portfolio, SymbolList.class);
        }
        if (removeST) {
            List<Stock> list = symbolList.getSymbols().stream().filter(i -> {
                return !i.getName().contains("ST");
            }).collect(Collectors.toList());
            symbolList.setSymbols(list);
        }
        return symbolList;
    }
    public Securities getStockInMinutes(String code, String t) {
//        /data/kline/{t}/{code}
        try {
            Securities stock = restTemplate.getForObject(propertyConfig.getSpiderService() + "/data/kline/" + t + "/" + code, Securities.class);
            return stock;
        } catch (Exception e) {
            logger.error(code);
            logger.error(e.getMessage(), e);
        }
        return null;
    }
}
